package com.yuan.app.pms.service.impl;

import com.yuan.app.pms.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.common.utils.PageUtils;
import com.yuan.common.utils.Query;

import com.yuan.app.pms.dao.BrandDao;
import com.yuan.app.pms.entity.BrandEntity;
import com.yuan.app.pms.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        if (StringUtils.isEmpty(key)) {
            IPage<BrandEntity> page = this.page(
                    new Query<BrandEntity>().getPage(params),
                    new QueryWrapper<BrandEntity>()
            );
            return new PageUtils(page);
        } else {
            IPage<BrandEntity> page = this.page(
                    new Query<BrandEntity>().getPage(params)
                    , new QueryWrapper<BrandEntity>().eq("brand_id", key).or()
                            .like("name", key)
            );
            return new PageUtils(page);
        }

    }

    @Override
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);

        //同步其他数据相关表
        if (!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateDetail(brand.getBrandId(),brand.getName());
        }

        //TODO 还要修改其他相关表
    }

}